#!/bin/bash

set -ux

# 网络
hostname=AnolisOS

ip=192.168.20.130
gateway=192.168.20.2
dns1=192.168.20.2

net=$(ip a | grep ens | tail -1 | awk '{print $NF}')
ifcfg=/etc/sysconfig/network-scripts/ifcfg-$net

echo "设置hostname $hostname"
hostnamectl --static set-hostname $hostname

echo '修改ssh配置'
sed -i -e '/^#UseDNS/c UseDNS no' \
  -e '/^GSSAPIAuthentication/c GSSAPIAuthentication no' \
  /etc/ssh/sshd_config &&
  systemctl restart sshd

echo "修改ip $ifcfg"
# 删除和修改
sed -i \
  -e "/^IPADDR=/d" \
  -e "/^PREFIX=/d" \
  -e "/^GATEWAY=/d" \
  -e "/^DNS1=/d" \
  -e '/^ONBOOT=*/c ONBOOT="yes"' \
  -e '/^BOOTPROTO=*/c BOOTPROTO="static"' \
  $ifcfg

# 添加
cat >>$ifcfg <<EOF
IPADDR="$ip"
PREFIX=24
GATEWAY="$gateway"
DNS1="$dns1"
EOF

echo '配置IP HOST映射关系'
cat >>/etc/hosts <<EOF
$ip $hostname
EOF

echo "查看 $ifcfg"
cat $ifcfg

echo '重启网卡'
nmcli c reload && nmcli c up $net
